package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2023-11-15
 * Time: 22:16
 */
public class AddOperation implements IOperation {
    //新增图书
    public void work(BookList bookList) {
        System.out.println("新增图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要新增图书的书名:");
        String name = scanner.nextLine();

        System.out.println("请输入你要新增图书的作者:");
        String author = scanner.nextLine();

        System.out.println("请输入你要新增图书的价格:");
        int price = scanner.nextInt();

        System.out.println("请输入你要新增图书的类型:");
        scanner.nextLine();//避免下面的语句把回车拿到
        String type = scanner.nextLine();

        //此时构建一个书的对象
        Book book = new Book(name, author, price, type);

        int currenSize = bookList.getUsedSize();

        for (int i = 0; i < currenSize; i++) {
            Book tmp = bookList.getBooks(i);
            if (tmp.getName().equals(name)) {
                System.out.println("存在这本书,不能重复添加");
                return;
            }
            //没有重复的书开始新增
            bookList.setBooks(book,currenSize);
            bookList.setUsedSize(currenSize+1); //下标加一
        }
    }
}
